<template>
  <div class="app-container">
    <el-form :model="queryParams"
             ref="queryForm"
             :inline="true"
             v-show="showSearch"
             label-width="68px">
      <el-form-item label="锁token"
                    prop="lockId">
        <el-input v-model="queryParams.lockId"
                  placeholder="请输入锁token"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
	  <el-form-item label="锁名称"
                    prop="deviceName">
        <el-input v-model="queryParams.deviceName"
                  placeholder="请输入锁名称"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="密码类型"
                    prop="pwdType">
        <el-select v-model="queryParams.pwdType"
                   placeholder="请选择密码类型"
                   clearable
                   size="small">
          <el-option label="临时密码"
                     value="1" />
          <el-option label="永久密码"
                     value="2" />
          <el-option label="授权1次密码"
                     value="3" />
        </el-select>
      </el-form-item>

      <el-form-item label="设备名称"
                    prop="deviceName">
        <el-input v-model="queryParams.deviceName"
                  placeholder="请输入锁名称"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="开始时间"
                    prop="startTime">
        <!-- <el-date-picker clearable size="small"
                        v-model="queryParams.startTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择开始时间"
        > -->
        <el-date-picker v-model="queryParams.startTime"
                        type="datetime"
                        placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间"
                    prop="endTime">
        <!-- <el-date-picker clearable size="small"
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择关闭时间"
        > -->
        <el-date-picker v-model="queryParams.endTime"
                        type="datetime"
                        placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"
                   icon="el-icon-search"
                   size="mini"
                   @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"
                   size="mini"
                   @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10"
            class="mb8">
      <el-col :span="1.5">
        <el-button type="primary"
                   plain
                   icon="el-icon-plus"
                   size="mini"
                   @click="handleAdd"
                   v-hasPermi="['system:pwd:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success"
                   plain
                   icon="el-icon-edit"
                   size="mini"
                   :disabled="single"
                   @click="handleUpdate"
                   v-hasPermi="['system:pwd:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger"
                   plain
                   icon="el-icon-delete"
                   size="mini"
                   :disabled="multiple"
                   @click="handleDelete"
                   v-hasPermi="['system:pwd:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning"
                   plain
                   icon="el-icon-download"
                   size="mini"
                   @click="handleExport"
                   v-hasPermi="['system:pwd:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch"
                     @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"
              :data="pwdList"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection"
                       width="55"
                       align="center" />
      <el-table-column label="id"
                       align="center"
                       prop="id"
                       v-if="false" />
      <el-table-column label="锁token"
                       align="center"
                       prop="lockId" />
      <el-table-column label="密码类型"
                       align="center"
                       prop="pwdType">
        <template slot-scope="scope">
          <span v-if="scope.row.pwdType==1">临时密码</span>
          <span v-else-if="scope.row.pwdType==2">永久密码</span>
          <span v-else-if="scope.row.pwdType==3">授权1次密码</span>
        </template>
      </el-table-column>
      <el-table-column label="密码"
                       align="center"
                       prop="cmdContext">
        <template slot-scope="scope">
          <span v-if="scope.row.cmdContext">{{scope.row.cmdContext}}</span>
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column label="设备名称"
                       align="center"
                       prop=" deviceName">
        <template slot-scope="scope">
          <span v-if="scope.row.deviceName">{{scope.row.deviceName}}</span>
          <span v-else>无</span>
        </template>
      </el-table-column>

      <el-table-column label="开始时间"
                       align="center"
                       prop="startTime"
                       width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间"
                       align="center"
                       prop="endTime"
                       width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="锁密码 " align="center" prop="pitNum"/> -->
      <el-table-column label="锁类型"
                       align="center"
                       prop="lockType">
        <template slot-scope="scope">
          <span v-if="scope.row.lockType==1">电子锁</span>
          <span v-else-if="scope.row.lockType==2">指纹锁</span>
        </template>
      </el-table-column>
      <el-table-column label="密码状态"
                       align="center"
                       prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status==1">已启用</span>
          <span v-else-if="scope.row.status==2">已过期</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名称"
                       align="center"
                       prop="createName" />
      <el-table-column label="操作"
                       align="center"
                       class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:pwd:edit']">修改
          </el-button>
          <el-button size="mini"
                     type="text"
                     icon="el-icon-delete"
                     @click="handleDelete(scope.row)"
                     v-hasPermi="['system:pwd:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <!-- 添加或修改锁密码对应对话框 -->
    <el-dialog :title="title"
               :visible.sync="open"
               width="500px"
               append-to-body>
      <el-form ref="form"
               :model="form"
               :rules="rules"
               label-width="80px">
        <!-- todo 这里查询锁列表 选择锁id-->
        <el-form-item label="锁token"
                      prop="lockId">
          <!-- <el-input v-model="form.lockId"
                    placeholder="请输入系统锁表id" /> -->
                    <el-select v-model="form.lockId"  placeholder="请选择">
                      <el-option
                        v-for="item in startLockList"
                        :key="item.lockToken"
                        :label="item.lockName"
                        :value="item.lockToken">
                      </el-option>
                    </el-select>
        </el-form-item>
        <el-form-item label="密码类型"
                      prop="pwdType">
          <el-select v-model="form.pwdType"
                     placeholder="请选择密码类型">
            <el-option label="临时周期密码"
			                  :key='1'
                       :value="1" />
            <el-option label="永久密码"
						            :key='2'
                       :value="2" />
            <el-option label="授权1次密码"
						            :key='3'
                       :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="开锁时间"
                      prop="startTime"
                      v-if="form.pwdType=='1'">
          <el-date-picker v-model="form.startTime"
                          type="datetime"
                          placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="关闭时间"
                      prop="endTime"
                      v-if="form.pwdType=='1'">
          <el-date-picker v-model="form.endTime"
                          type="datetime"
                          placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="卡号密码"
                      prop="cmdContext">
          <el-input v-model="form.cmdContext"
                    placeholder="请输入卡号或者密码" />
        </el-form-item>

      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button type="primary"
                   @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPwd, getPwd, delPwd, addPwd, updatePwd, exportPwd, trunOnLocks } from '@/api/lock/lockPwd'

export default {
  name: 'Pwd',
  components: {},
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 锁密码对应表格数据
      pwdList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lockId: undefined,
        pwdType: undefined,
        cmdType: undefined,
        cmdContext: undefined,
        startTime: undefined,
        endTime: undefined,
        lockType: undefined,
        pitNum: undefined,
        status: undefined,
        createName: undefined,
        deviceName:''
      },
	  startLockList:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cmdContext: [
          { required: true, message: '请输入6位数字卡号密码', trigger: 'blur' },
          {
            pattern: /^\d{6}$/,
            message: '请输入6位数字卡号密码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    /** 查询锁密码对应列表 */
    getList () {
      this.loading = true
      listPwd(this.queryParams).then(response => {
        this.pwdList = response.rows
        this.total = response.total
        this.loading = false
        console.log(response)
      })
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        lockId: undefined,
        pwdType: undefined,
        cmdType: undefined,
        cmdContext: undefined,
        startTime: undefined,
        endTime: undefined,
        lockType: undefined,
        pitNum: undefined,
        status: undefined,
        createTime: undefined,
        updateTime: undefined,
        createBy: undefined,
        createName: undefined,
        deviceName:undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.title = '添加锁密码对应'
	  trunOnLocks().then((response)=>{
		  this.startLockList=response.data;
	  })

    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      const id = row.id || this.ids
      getPwd(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改锁密码对应'
      })
	  trunOnLocks().then(response=>{
	  		  this.startLockList=response.data;
	  })
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePwd(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addPwd(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除锁密码对应编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delPwd(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有锁密码对应数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return exportPwd(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    }
  }
}
</script>
